<?php
declare (strict_types = 0);
namespace app\controller;

use think\facade\View;
use think\facade\Db;
use think\Validate;
use think\facade\Request;
use think\facade\Session;
use app\model\UserModel;
use app\model\CatModel;
use app\Common;
use app\controller\Token;

class Assess{

    //常用数据表内容 及常用字典
    var $CarType=[];
    var $car=[];
    var $users =[];
    var $status=array('1'=>'正常','2'=>'停用');
    var $back = "<script>setTimeout(function(){history.back();},3000)</script>";
    var $stars = array(1=>'★',2=>'★★',3=>'★★★',4=>'★★★★',5=>'★★★★★');

    public function __construct(){
        //准备一个类型数组

        $gm=new CatModel();
        $list = $gm->catTree(1); 

         foreach($list as $k=>$v){
            $this->CarType[$v['id']]=$v;
         }        

        $um=new UserModel();
        $this->users = $um->getAll();
        
   
    }

    public function getImgs($imgs=''){
        if(!$imgs) return '';
        $arr = explode(",",$imgs);
        return implode(",",$arr);
    }


    public function index(){
        $uid=Session::get('logonid');
        if(!$uid) return header("location:/index.php/login");
        $name = $mobile=$cid='';
        $all = Request::param();

        $list = Db::table('assess');
        if(!empty($all['name'])){
            $name =$all['name'];
            $cid = Db::table('customer')->where('name',$name)->value('id');
            if($cid) $list->where('to',$cid);
        }
        if(!$cid && !empty($all['mobile'])){
            $mobile =$all['mobile'];
            $cid = Db::table('customer')->where('mobile',$mobile)->value('id');
            if($cid) $list->where('to',$cid);
        }

        $sort = 'val_total';
        if(!empty($all['sort'])){
            $sort = 'val_'.$all['sort'];
        }

        $list=$list->order($sort,'desc')->paginate(100)->toArray();

        //halt($list);

        foreach($list['data'] as &$v){
            $v['to_name'] =Db::table('customer')->where('id',$v['to'])->value('name');
            $v['from_name']=Db::table('customer')->where('id',$v['from'])->value('name');
            $v['order_no']=Db::table('order')->where('id',$v['order_id'])->value('order_no');
            $v['val_5'] = $v['val_total'];
            $v['v1_star'] = $this->stars[$v['val_1']];
            $v['v2_star'] = $this->stars[$v['val_2']];
            $v['v3_star'] = $this->stars[$v['val_3']];
            $v['v4_star'] = $this->stars[$v['val_4']];

            $v['stars'] = $this->stars[$v['val_5']];
        }
        
        View::assign(['list'=>$list,'name'=>$name,'mobile'=>$mobile]);
        return View::fetch('assess_index');
    }

    public function edit(){
    }

    /*
    *name:assess_add
    *desc:评价提交接口
    *参数:{token:'xx..',cid:教练id,val_1:n,val_2:n,val_3:n,val_4:n,text:'text'}
    *返回:{code:1/0,msg:'ok/fail'}
    */
    public function assess_add(){
        $all = Request::param();

        if(empty($all['token'])) return $this->post_res('无效的token!');
        $token =new Token();
        $arr = $token->check_token($all['token']);
        if(!$arr['code']) return $this->post_res($arr['msg']);
        $me = $arr['customer'];

        //if(empty($all['cid'])) return $this->post_res('无效的评价对象!');
        //评价教练
        if(!empty($all['cid'])){
            $cust = Db::table('customer')->find($all['cid']);
            if(!$cust) return $this->post_res('找不到评价对象！');
            $row = Db::table('assess')->where('from',$me['id'])->where('to',$all['cid'])->where('order_id','<','1')->value('id');
            if($row){
                return $this->post_res('你已经对这位教练评价过了！');
            }
        }

        $order_id=0;
        if(!empty($all['order_no'])){
            $order_id=Db::table('order')->where('order_no',$all['order_no'])->value('id');
        }

        $arr=array(
            'from'=>$me['id'],
            'order_id'=>$order_id,
            'val_1'=>!empty($all['val_1'])?$all['val_1']:1,
            'val_2'=>!empty($all['val_2'])?$all['val_2']:1,
            'val_3'=>!empty($all['val_3'])?$all['val_3']:1,
            'val_4'=>!empty($all['val_4'])?$all['val_4']:1,
            'val_total' => round(($all['val_1']+$all['val_2']+$all['val_3']+$all['val_4'])/4),
            'create_time'=>date('Y-m-d H:i:s'),
            'text'=>!empty($all['text'])?$all['text']:'这家伙很懒...'
        );
        if($cust) $arr['to']=$cust['id'];   //评价的人员对象

        $res =Db::table('assess')->insert($arr);
        if($res) {
            if($order_id){
                Db::table('order')->update(array('id'=>$order_id,'status'=>'5'));
            }
            //有评价对象，就更新一下，一般商品订单的评价不更新。
            if($cust){
                $ass =Db::query('select count(id) as count,sum(val_total) as sum from assess where `to`=?',[$cust['id']]);
                if($ass[0]['count']){
                    $stars  = round($ass[0]['sum']/$ass[0]['count']);
                    Db::execute('update customer set level='.$stars.' where id='.$cust['id']);
                }
            }

            return json(['code'=>1,'msg'=>'ok']);
        }
        return $this->post_res();
    
    }

    public function assess(){

    }

    //商品的评价
    public function goods_assess(){
        $all=Request::param();
        if(empty($all['goods_id'])){
            return json(['code'=>0,'msg'=>'缺少商品id参数:goods_id']);
        }

        $sql = "select * from assess where order_id in(select order_id from order_list where goods_id=?)";
        $list =Db::query($sql,[$all['goods_id']]);

        return json(['code'=>1,'msg'=>$list]);
    }

    public function post_res($msg=''){
        if(!$msg) $msg = '无资料！';
        return json(['code'=>0,'msg'=>$msg]);
    }


    


}
